<style scoped>
.address_list{padding: 0 12px;}
.address_list>*{margin: 10px 0;}
.address_row{padding: 6px; background: var(--van-cell-background);border-radius: 8px;}
.address_row .big{font-size: 1.1em;font-weight: bold;margin-bottom: 2px;}
.address_row .van-icon{font-size: 1.3em;padding: 0 5px;}
</style>
<template>
  <HeaderSlot>
    <van-nav-bar :title="$t('我的地址')" left-arrow :right-text="$t('添加')" @click-left="href_handle(-1)" @click-right="href_handle('/user/address_edit')" />
  </HeaderSlot>
  <div class="address_list">
    <div class="flex address_row" v-for="row in list">
      <div class="max" @click="click(row)">
        <div class="flex big" style="justify-content: space-between">
          <p>{{ row.name }} {{ row.phone }}</p>
          <van-tag v-if="row.default==1" type="primary">默认</van-tag>
        </div>
        <div class="info">{{row.area_name}} {{row.address}}</div>
      </div>
      <van-icon v-show="row.type==0" name="edit" @click="href_handle('./address_edit?id='+row.id)" />
      <van-icon v-show="row.type==0" name="delete-o" @click="del(row)"/>
    </div>
  </div>
</template>

<script>
import {msg} from "@/js/vant_tool.js";
import {_get, href_handle} from "@/vue_plugin/router_auto.js";
import {ajax} from "@/js/api_tool.js";
import HeaderSlot from "@/components/slot/HeaderSlot.vue";

export default {
  components: {HeaderSlot},
  data(){
    let type = _get("type", 0);
    let is_select = _get("is_select", 0);
    return {
      type: type,
      is_select: is_select,
      list: [],
      callback: null,
    }
  },
  methods:{
    href_handle,
    init(type){
      if (typeof type !== 'undefined') this.type = type;
      ajax("/home/address/list?type="+this.type, ({data})=>{
        this.list = data;
      })
    },
    del(row){
      msg.confirm("是否确定删除此地址").then(()=>{
        ajax({url:"/home/address/del?ids="+row.id, loading:1}, ({data})=>{
          if (data>0) this.init();
        })
      }).catch(()=>{});
    },
    click(row){
      if(!this.is_select) return;
      if (typeof this.callback === 'function') this.callback(row);
    },
  },
  computed:{},
  mounted(){
    this.init();

  }
}
</script>